package org.strongme.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.persistence.Page;
import org.strongme.common.util.excel.ExportExcel;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.AssGet;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.SubProject;
import org.strongme.modules.project.service.AssGetService;
import org.strongme.modules.project.service.ProjectService;
import org.strongme.modules.project.service.SubProjectService;
import org.strongme.util.DateUtils;
import org.strongme.util.StringUtils;

import com.google.common.collect.Lists;

@Controller
@RequestMapping("project/assGet")
public class AssGetController extends BaseController{
	
	@Autowired
	private AssGetService assGetService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private SubProjectService subProjectService;
	
	@ModelAttribute("assGet")
	public AssGet get(@RequestParam(required=false) String id) {
		AssGet assGet = null;
		if (StringUtils.isNotBlank(id)){
			assGet = assGetService.get(new AssGet(id));
		}else{
			assGet = new AssGet();
		}
		return assGet;
	}
	
	@RequestMapping(value = {"list",""})
	public String list(AssGet assGet, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AssGet> page = assGetService.findPage(new Page<AssGet>(request, response), assGet); 
        model.addAttribute("page", page);
        model.addAttribute("assGet", assGet);
		return "modules/project/assGet/assGetList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(AssGet assGet, Model model) {
		model.addAttribute("assGet", assGet);
		model.addAttribute("projectList", projectService.findList(new Project()));
		return "modules/project/assGet/assGetForm.jsp";
	}
	
	@RequestMapping(value = "save" ,method=RequestMethod.POST)
	public String save(AssGet assGet, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, assGet)){
			return form(assGet, model);
		}
		assGetService.save(assGet);
		addMessage(redirectAttributes, "保存资料领取数据成功");
		return "redirect:/project/assGet";
	}
	
	@RequestMapping(value = "delete" ,method=RequestMethod.GET)
	public String delete(AssGet assGet, Model model, RedirectAttributes redirectAttributes) {
		assGetService.delete(assGet);
		addMessage(redirectAttributes, "删除资料领取数据成功");
		return "redirect:/project/assGet";
	}
	
	@RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(AssGet assGet, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = new String(("材料领取记录").getBytes("gbk"),"iso8859-1")+DateUtils.getDate()+".xlsx";
            List<AssGet> list = assGetService.findList(assGet);
            List<AssGet> newResult = Lists.newArrayList();
            for(int i=0;i<list.size();i++) {
            	AssGet ag = list.get(i);
            	ag.setXh(i+1);
            	newResult.add(ag);
            }
            List<String> subTitles = Lists.newArrayList();
            String sub = "";
            if(newResult.size()>0) {
            	AssGet ag = newResult.get(0);
            	Project p = projectService.get(ag.getProject());
            	SubProject sp = subProjectService.get(ag.getSubProject());
            	if(p!=null &&p.getName()!=null&&sp!=null&&sp.getName()!=null)
        		sub += "工程名称："+p.getName()+"\u0020\u0020\u0020\u0020\u0020\u0020单位工程："+sp.getName();
            }
            String zz = assGet.getZz()==null?"":assGet.getZz();
            String gh = assGet.getGh()==null?"":assGet.getGh();
            String jcks = assGet.getKsrq()==null?"":assGet.getKsrq();
            String jcjs = assGet.getJsrq()==null?"":assGet.getJsrq();
//            sub+="\u0020\u0020\u0020\u0020\u0020\u0020装置："+zz;
//            sub+="\u0020\u0020\u0020\u0020\u0020\u0020工号："+gh;
            sub+="\u0020\u0020\u0020\u0020\u0020\u0020领取日期："+jcks+"\u0020到\u0020"+jcjs;
            subTitles.add(sub);
    		new ExportExcel("材料领取记录",subTitles, AssGet.class).setDataList(newResult).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出资料领取数据失败！失败信息："+e.getMessage());
		}
		return "redirect:/project/assGet";
    }
	

}
